g total_issues =$ni

for any $issues : replace lp_X_$year_1= -99 if lp_X_$year_1 ==. & missing_X_1 == 1 // re classify as don't know people who were missing on the opinion
for any $issues : replace lp_X_$year_2= -99 if lp_X_$year_2 ==. & missing_X_2 == 1
for any $issues : replace rp_X_$year_1= -99 if rp_X_$year_1 ==. & missing_X_1 == 1
for any $issues : replace rp_X_$year_2= -99 if rp_X_$year_2 ==. & missing_X_2 == 1

egen missingc_1 =rowtotal(missing_*_1)
egen missingc_2 =rowtotal(missing_*_2)
tab1  missingc_*
*drop if  missingc_1 > $ni *0.754 // keep 75% or less missing
*drop if missingc_2 > $ni *0.754 // keep 75% or less missing

if $ktype == 1 {
	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

}
if $ktype == 2 {    /// same as 1 but code folks as correct if they place their own party/candidate on the right side
  	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

	for any $issues : replace kni_X = 1 if kni_X ==0 & ///
		(PID_1 ==1  & rp_X_$year_1>4 & PID_2 ==1  & rp_X_$year_2>4   |  ///   Republicans
		 PID_1 ==0  & lp_X_$year_1<4 & PID_2 ==0  & lp_X_$year_2<4   |  ///   Democrats
		 vote_1 ==1 & rp_X_$year_1>4 & vote_2 ==1 & rp_X_$year_2>4 | ///  Republican voters
		 vote_1 ==0 & lp_X_$year_1<4 & vote_2 ==0 & lp_X_$year_2<4 ) //Democratic voters											   
}  
if $ktype == 3 {    /// code people who put parties/candidates at the midpoint as incorrect
  	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

	for any $issues : replace kni_X = 0 if  ///
		 rp_X_$year_1== 4  | rp_X_$year_2== 4   | lp_X_$year_1== 4 | lp_X_$year_2<4  										   
}  
if $ktype == 4 {    
  	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

	for any $issues : replace kni_X = 0 if  ///
		 rp_X_$year_1== 4& PID_1 == 1  & rp_X_$year_2== 4 & PID_2 == 1 | lp_X_$year_1== 4& PID_1 == 0 & lp_X_$year_2<4& PID_2 == 0										   
}  
if $ktype == 5 {    
  	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

	for any $issues : replace kni_X = 0 if  ///
		 rp_X_$year_1== 4& PID_1 == 1  & rp_X_$year_2== 4 & PID_2 == 1 & lp_X_$year_1== 4& PID_1 == 0 & lp_X_$year_2<4& PID_2 == 0										   
}  

if $ktype == 6 {    
  	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

	for any $issues : replace kni_X = -1 if  ///
              lp_X_$year_1 > rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
             & lp_X_$year_2 > rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 & rp_X_$year_1<. & rp_X_$year_2<.
}  

if $ktype == 7 {    // same as type I except correct placements 1 
  	capture drop kni_*
    for any $issues : g kni_X = lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 < rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

	for any $issues : replace kni_X =.5 if kni_X==0 & lp_X_$year_1 < rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99  & rp_X_$year_1<. & rp_X_$year_2<.
}  

if $ktype == 8 { // consistently reverse positions
	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 > rp_X_$year_1 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 > rp_X_$year_2 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if lp_X_$year_1<. & lp_X_$year_2<.

}
if $ktype == 9 { // put them on the right side of the scale
	capture drop kni_*
	for any $issues : g kni_X = lp_X_$year_1 <4 & rp_X_$year_1 >4 & lp_X_$year_1 !=-99 & rp_X_$year_1 !=-99 /// 
                              & lp_X_$year_2 <4 & rp_X_$year_2 >4 & lp_X_$year_2 !=-99 & rp_X_$year_2 !=-99 if rp_X_$year_1<. & rp_X_$year_2<.

}

for any $issues : drop if kni_X ==.  //  Drop cases where we are missing  placements

*standardize
capture drop std_*
if $standardized == 1 {
	for any $issues : egen X_$year_1 = std(s_X_$year_1)
	for any $issues : egen X_$year_2 = std(s_X_$year_2) 
}
if $standardized == 0 {
	for any $issues : g X_$year_1 = s_X_$year_1
	for any $issues : g X_$year_2 = s_X_$year_2
}
rename therm_$year_1 therm_1									  
rename therm_$year_2 therm_2
norm therm_1
norm therm_2									  
g therm_change_absolute =abs(therm_2 - therm_1)

*create version of position knowledge that incorporates  *having a party ID that is stable *agreeing with your party in wave 1
*  for any $issues: g notakers_X = PID_1 ==.5 | PID_2 == .5   ///
                                      | PID_1 != PID_2             ///
		            				  | PID_1 == 1 & s_X_$year_1 <0  ///
								   	  | PID_1 == 0 & s_X_$year_1 >1  & s_X_$year_1 <.
* for any $issues: g notakers_X =   PID_1 != PID_2     
 for any $issues: g notakers_X =   therm_change_absolute <=.75  
 
if $no_takers == 1 {
  *for any $issues: replace kni_X = 0 if notakers_X == 1
  for any $issues: replace kni_X = 0 if  therm_change_absolute <=.75 
}
*calculate total knowledge of placements
global knowledge_string
  foreach w of global issues {  // This loop just adds year to the end of each other variables
    global knowledge_string $knowledge_string kni_`w'
  }
egen total_knowledge = rowtotal($knowledge_string)

save Data/temporary/temporary, replace

*displays number of combinations (and therefore regressions to be run)
tuples $issues , display

global issues1
global issues2
foreach w of global issues { // This loop just adds year to the end of each other variables
  global issues1 $issues1 `w'_$year_1
  global issues2 $issues2 `w'_$year_2
}
sum $issues1
corr $issues1
sum $issues2
corr $issues2

*create one version of the data set for each possible scale
* single items scales 
forval i=1/$ni {
  use Data/temporary/temporary, clear
  display "`tuple`i''"
  g change =  abs(`tuple`i''_$year_2-`tuple`i''_$year_1)
  g scale_1 = `tuple`i''_$year_1
  g scale_2 = `tuple`i''_$year_2
  g missing_1 = missing_`tuple`i''_1
  g missing_2 = missing_`tuple`i''_2
  *treats midpoint as crystallized
    *g crystallized =  (s_`tuple`i''_$year_1 >4 & s_`tuple`i''_$year_2 >4  |  s_`tuple`i''_$year_1 <4 & s_`tuple`i''_$year_2 <4  |  s_`tuple`i''_$year_1 ==4 & s_`tuple`i''_$year_2 ==4) &  s_`tuple`i''_$year_1 !=-99 & s_`tuple`i''_$year_2 != -99
  *treats midpoint as not crystallized
     g crystallized =  (s_`tuple`i''_$year_1 >4 & s_`tuple`i''_$year_2 >4  |  s_`tuple`i''_$year_1 <4 & s_`tuple`i''_$year_2 <4)  & s_`tuple`i''_$year_1 !=4 & s_`tuple`i''_$year_2 !=4 &  missing_`tuple`i''_1 != 1 & missing_`tuple`i''_2 != 1
  g issues_scale = "`tuple`i''"
  g k_scale= kni_`tuple`i''
  g t =`i'
	*if $partial_learners == 1 {
      *g k_partial =1
	  *g agree_candidate = scale_1>4 & therm_1 > 0.5 | scale_1<4 & therm_1<0.5  if scale_1 <. & scale_2 <. & PID_1<.
	  *g agree_party = scale_1>4 & PID_1==1 | scale_1<4 & PID_1==0  if scale_1 <. & scale_2 <. & PID_1<.
	 *}						
  keep ID change issues_scale k_scale t total_knowledge gk scale_* PID_1 PID_2 therm_1 therm_2 weight crystallized total_issues missing_1 missing_2  vote_1 vote_2 missingc_1 missingc_2
  save Data/temporary/temporary_`i' , replace
}

*multi-item scales
local n=$ni+1
forval i=`n'/`ntuples' {
  use Data/temporary/temporary, clear
  global issues1
  global issues2
  display "`tuple`i''"
  foreach w of local tuple`i' {  // This loop just adds year to the end of each other variables
    global issues1 $issues1 `w'_$year_1
    global issues2 $issues2 `w'_$year_2
    display "`w'"
    display "$issues1"
  }
  if $factor == 1 {
	factor $issues1, pcf
	predict scale_1
	factor $issues2, pcf
	predict scale_2
  }
  if $factor == 0 {
	egen scale_1= rowmean($issues1)
	egen scale_2= rowmean($issues2)
  }
  g change =  abs(scale_1-scale_2 )
  global knowledge_string
  foreach w of local tuple`i' {  // This loop just adds year to the end of each other variables
    global knowledge_string $knowledge_string kni_`w'
  }
  egen k_scale = rowtotal($knowledge_string)
  g issues_scale = "`tuple`i''"
  g t =`i'
  keep ID change issues_scale k_scale t total_knowledge  gk  scale_*  PID_1 PID_2 therm_1 therm_2 weight total_issues  vote_1 vote_2 
  save Data/temporary/temporary_`i' , replace
}
 
use Data/temporary/temporary_1, clear
forval i=2/`ntuples'{
  append using Data/temporary/temporary_`i' 
  erase Data/temporary/temporary_`i'.dta 
}

g NUM_issues = wordcount(issues_scale)
sum   NUM_issues k_scale

g study ="$study"
*drop if k_scale==.

		 
save Data/temporary/stability_pooled_$study , replace















